package itstudio.instructor.adapter;

import itstudio.app.wq.R;
import itstudio.instructor.config.MyApplication;
import itstudio.instructor.entity.Comment;
import itstudio.instructor.util.TimeUtil;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MessageAdapter extends BaseAdapter {
	private final class ViewHolder {
		public TextView message_author;
		public TextView message_title;
		public TextView message_time;
		public TextView message_content;
	}

	private LayoutInflater mInflater;
	private List<Comment> commentList =new ArrayList<Comment>();
	private Comment comment;
	private int layout;
	ViewHolder holder; 

	public MessageAdapter(Context context, List<Comment> listStrategies,int layout) {
		if(listStrategies!=null){
			
			this.commentList = listStrategies;
		}
		this.layout = layout;
		this.mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return commentList ==null ? 0:commentList.size();
	}

	
	@Override
	public Object getItem(int position) {
		
		return commentList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public int appendData(List<Comment> addList,boolean refesh) {
		int result =0;
		if (addList == null || addList.size()==0) {
			result = commentList.size()/10;
			if(commentList.size()%10!=0){
				result++;
			}
			return result;
		}
		// 首次加载
		if(commentList.size()==0){
			commentList.addAll(addList);//在加载新的
			notifyDataSetChanged();
			return 1; //当前是第一页
		}
		// 有新的数据
		if(!commentList.get(0).getId().equals(addList.get(0).getId())){
			
			if(refesh){
				commentList.clear();  //  先清空
				commentList.addAll(addList);//在加载新的
				notifyDataSetChanged();
				return 1;
			}else{
				commentList.addAll(addList);//直接追加
				notifyDataSetChanged();
				result = commentList.size()/10;
				if(commentList.size()%10!=0){
					result++;
				}
				return result;
			}
		}
		// 没有新的数据
		result = commentList.size()/10;
		if(commentList.size()%10!=0){
			result++;
		}
		return result;
	
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;

		comment = commentList.get(position);
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(layout,
					null);
			holder.message_author = (TextView) convertView.findViewById(R.id.message_authorName);
			holder.message_title = (TextView) convertView.findViewById(R.id.message_title);
			holder.message_time = (TextView) convertView.findViewById(R.id.message_time);
			holder.message_content = (TextView) convertView.findViewById(R.id.message_content);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		if(comment.getAuthorId().equals(MyApplication.user.getId())){
			// 我的评论
			holder.message_author.setText(R.string.me);
		}
		else{
			holder.message_author.setText(comment.getAuthorName());
		}
		holder.message_title.setText(comment.getNews().getTitle());
		holder.message_time.setText(TimeUtil.dateToString(comment.getPostTime(), TimeUtil.FORMAT_DATE));
		holder.message_content.setText(comment.getContent());
		return convertView;
	}

	
}
